﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Data;
using System.Linq;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YCAEK03DOC : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public YCAEK03DOCData m_Data, m_DataX;
        public DataSet dsParamaters;
        public YCAEK03DOCReport m_rpt;
        bool IsSupplierLoded = false;
        public string PrnType = "EXCEL";
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB Set4ITX
        {
            get
            {
                return (SETINTEXDB)TryFindResource("Set4ITX");
            }
        }

        #region=====vwLIEFDAT=======
        CollectionViewSource vwLIEFDAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwLIEFDAT");
            }
        }
        DataRow dwLIEFDAT
        {
            get
            {
                if (vwLIEFDAT.View == null)
                    return null;
                if ((DataRowView)vwLIEFDAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwLIEFDAT.View.CurrentItem).Row;
            }
        }
        #endregion

        #region=====vwEKKOPF=======
        CollectionViewSource vwEKKOPF
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKKOPF");
            }
        }
        DataRow dwEKKOPF
        {
            get
            {
                if (vwEKKOPF.View == null)
                    return null;
                if ((DataRowView)vwEKKOPF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKKOPF.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwEKPOS=======
        CollectionViewSource vwEKPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKPOS");
            }
        }
        DataRow dwEKPOS
        {
            get
            {
                if (vwEKPOS.View == null)
                    return null;
                if ((DataRowView)vwEKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKPOS.View.CurrentItem).Row;
            }
        }
        #endregion
        bool IsChkALL
        {
            get
            {
                if (ChkALL.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
            set
            {
                if (value)
                {
                    ChkALL.IsChecked = true;
                }
                else
                {
                    ChkALL.IsChecked = false;
                }
            }
        }
        bool IsChkEKPBEKZ
        {
            get
            {
                if (ChkEKPBEKZ.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
            set
            {
                if (value)
                {
                    ChkEKPBEKZ.IsChecked = true;
                }
                else
                {
                    ChkEKPBEKZ.IsChecked = false;
                }
            }
        }
        bool IsChkEKPKNTRNRL
        {
            get
            {
                if (ChkEKPKNTRNRL.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
            set
            {
                if (value)
                {
                    ChkEKPKNTRNRL.IsChecked = true;
                }
                else
                {
                    ChkEKPKNTRNRL.IsChecked = false;
                }
            }
        }
        public string KRS
        {
            get
            {
                return m_Data.Right("  " + txtABPAUFKRS.Text.ToString().Trim(), 2);
            }
            set
            {
                txtABPAUFKRS.Text = value;
            }
        }
        public string LFD
        {
            get
            {
                return m_Data.Right("000000" + txtABPAUFLFD.Text.ToString().Trim(), 6); ;
            }
            set
            {
                txtABPAUFLFD.Text = value;
            }
        }
        #endregion
        public YCAEK03DOC()
        {
            InitializeComponent();
            this.Name = "采购单打印";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = false;
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];

            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            m_Data = new YCAEK03DOCData(OracleDB, proxy);
            m_Data.UserID = Helpers.Helper.Left((string)dicParamater["User"] + "       ", 8);
            txtABPAUFKRS.Text = "WP";
            txtABPAUFLFD.Text = "000150";
            IsChkALL = true;
            IsChkEKPBEKZ = false;
            IsChkEKPKNTRNRL = false;
            butSTARTAPP.IsEnabled = false;
            butENDAPP.IsEnabled = false;
            btnPrint.IsEnabled = false;
            btnPrintPreview.IsEnabled = false;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {

                if (!IsSupplierLoded)
                {
                    SetData.Merge(m_Data.LoadSupplier());
                    IsSupplierLoded = true;
                }

                vwEKKOPF.View.CurrentChanged += vwEKKOPF_CurrentChanged;
                vwLIEFDAT.View.CurrentChanged += vwLIEFDAT_CurrentChanged;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }

        void vwLIEFDAT_CurrentChanged(object sender, EventArgs e)
        {
            if (dwLIEFDAT == null)
                return;

            DataSet Result = new DataSet();
            Result = m_Data.GetOrderBySupplier(dwLIEFDAT);
            SetData.EKKOPF.Clear();
            SetData.Merge(Result, false, MissingSchemaAction.Add);
        }

        void vwEKKOPF_CurrentChanged(object sender, EventArgs e)
        {
            DataSet Result = new DataSet();
            DataRow dwInput;
            dwInput = dwEKKOPF;
            if (dwInput == null)
            {
                return;
            }
            Result = m_Data.BuildData(dwInput);
            Set4ITX.Clear();
            Set4ITX.Merge(Result, false, MissingSchemaAction.Add);
        }
        public void btnQuery_Click(object sender, EventArgs e)
        {
            DataSet Result = new DataSet();
            DataRow dwInput;
            dwInput = dwEKKOPF;
            try
            {
                if (dwInput == null)
                {
                    if (LFD.Trim() == "0000000")
                        return;
                    dwInput = SetData.EKPOS.NewRow();
                    dwInput["EKPMANDANT"] = "00";
                    dwInput["EKPAUFKRS"] = KRS;
                    dwInput["EKPAUFLFD"] = LFD;
                }
                if (IsChkEKPBEKZ)//已经确认数量，未付款
                {
                    dwInput["EKPBEKZ"] = "'N'";
                    IsChkALL = false;
                    dwInput["EKPKNTRNRL"] = " ";
                }
                if (IsChkALL)//未付款，未确认数量
                {
                    dwInput["EKPBEKZ"] = "' '";
                    IsChkEKPBEKZ = false;
                    dwInput["EKPKNTRNRL"] = " ";
                }
                if (IsChkEKPKNTRNRL)//已经付款
                {
                    dwInput["EKPBEKZ"] = "'N'";
                    IsChkALL = false;
                    dwInput["EKPKNTRNRL"] = "NOT";
                }
                Result = m_Data.BuildData(dwInput);
                Set4ITX.Clear();
                Set4ITX.Merge(Result);
                SetData.EKVRECH.Clear();
                SetData.Merge(Set4ITX.EKVRECH);
                Set4ITX.EKVRECH.Clear();
                if (vwEKPOS.View != null)
                {
                    vwEKPOS.View.MoveCurrentToFirst();
                }
                butSTARTAPP.IsEnabled = true;
                butENDAPP.IsEnabled = false;
                btnPrint.IsEnabled = true;
                btnPrintPreview.IsEnabled = true;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void btnPrintPreview_Click(object sender, EventArgs e)
        {
            PrnType = "OpenExcel";
            using (new LongOperation(this, "读取符合条件的记录"))
            {
                DataSet setRpt = new DataSet();

                if (setRpt == null)
                    return;
                m_DataX = new YCAEK03DOCData(dicParamater);
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                if (m_DataX.exAgent._app == null)
                {
                    m_DataX.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
                }
                m_rpt = new YCAEK03DOCReport(dicParamater);
                m_rpt.dsSource = Set4ITX;
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.OracleDB = this.OracleDB;
                m_rpt.proxy = this.proxy;
                m_rpt.WriteExcelReport(PrnType);
            }
        }
        private void StartApply(DataRow dwTemp)
        {
            if (dwTemp == null)
            {
                MessageBox.Show("请选择一条记录");
                return;
            }
            string ordernum = Helpers.Helper.Left(dwTemp["EKPAUFKRS"].ToString() + "-" + dwTemp["EKPAUFLFD"].ToString() + "-" + dwTemp["EKPAUFPOS"].ToString() + "                       ", 23);
            if (dwTemp["EKPBEKZ"].ToString().Trim() != "N")
            {
                MessageBox.Show(ordernum+" 采购员尚未确认!");
                return;
            }
            decimal applied = 0;
          var query = from DataRow row in Set4ITX.EKVRECH.AsEnumerable()
                    where row["EKVFILLER"].ToString() == ordernum
                    select row;
          if (query.Count() > 0)
          {
              MessageBox.Show(ordernum + " 不能重复请款!");
              return;
          } 
          
            decimal ekpwe1 = System.Convert.ToDecimal(dwTemp["EKPWE1"].ToString());
            string syear = Helpers.Helper.Right(m_Data.year, 2);
            query = from DataRow row in SetData.EKVRECH.AsEnumerable()
                        where row["EKVFILLER"].ToString() == ordernum
                        select row;
            foreach (DataRow row in query)
            {
                applied += System.Convert.ToDecimal(row["EKVBETRAG"].ToString());
            }
      
            if (ekpwe1 - applied <= 0)
            {
                MessageBox.Show(ordernum+" 已经完成请款!");
                return;
            }
            string EKVBELEG = m_Data.GetAUFLFD(syear, "EKAUFTR ");
            if (!EKVBELEG.StartsWith(m_Data.month))
            {
                EKVBELEG = m_Data.month + "0001";
                DataRow dwSDDAT = Set4ITX.SDDAT.NewRow();
                dwSDDAT["SDSA"] = "0022";
                dwSDDAT["SDKEY"] = "00EKAUFTR " + syear + "          00";
                dwSDDAT["SDDATEN"] = "BESTELLAUFTR.LFD-NR 0000000000199999999999" + "00000" + m_Data.month + "0001V  0000000" +
                    m_Data.date + "    000000000                                                   " + m_Data.UserID + m_Data.date + "INTEXE  201403040002*";
                m_Data.INTEXDBC.SaveSDDAT(dwSDDAT);
            }
            DataRow dwData = Set4ITX.EKVRECH.NewRow();
            dwData["EKVMANDANT"] = dwTemp["EKPMANDANT"];
            dwData["EKVABLAGE"] = syear;
            dwData["EKVBELEG"] = EKVBELEG;
            dwData["EKVLIEFNR"] = dwTemp["EKPLIEFNR"];
            dwData["EKVRECHNR"] = Helpers.Helper.Left(dwTemp["EKPAUFKRS"].ToString() + "-" + dwTemp["EKPAUFLFD"].ToString() + "           ", 10);
            dwData["EKVBETRAG"] = ekpwe1 - applied;
            dwData["EKVWAEHRSL"] = dwTemp["EKPWAEHRSL"];
            dwData["EKVKST"] = "0";
            dwData["EKVEMPF"] = m_Data.UserID;
            dwData["EKVKIR"] = " ";
            dwData["EKVABRECHKZ"] = " ";
            dwData["EKVFILLER"] = ordernum;
            dwData["EKVAENDZL"] = 1;
            dwData["EKVAENDBEN"] = "        ";
            dwData["EKVAENDDAT"] = 0;
            dwData["EKVNEUBEN"] = m_Data.UserID;
            dwData["EKVNEUDAT"] = m_Data.date;
            Set4ITX.EKVRECH.Rows.Add(dwData);
            vwEKPOS.View.MoveCurrentToNext();
            
        }
        public void cmdStartApply_Click(object sender, EventArgs e)
        {
            try
            {
                StartApply(dwEKPOS);
                butSTARTAPP.IsEnabled = true;
                butENDAPP.IsEnabled = true;
                btnPrint.IsEnabled = false;
                btnPrintPreview.IsEnabled = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void cmdStartAllApply_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (DataRow dwTemp in Set4ITX.ICC_EKPOS.Rows)
                {
                    StartApply(dwTemp);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        
        public void cmdEndApply_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (DataRow row in Set4ITX.EKVRECH.Rows)
                {
                    m_Data.INTEXDBC.SaveEKVRECH(row);
                }
                butSTARTAPP.IsEnabled = false;
                butENDAPP.IsEnabled = false;
                btnPrint.IsEnabled = true;
                btnPrintPreview.IsEnabled = true;
                Set4ITX.EKVRECH.Clear(); 
                btnQuery_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void btnPrint_Click(object sender, EventArgs e)
        {

            using (new LongOperation(this, "读取符合条件的记录"))
            {
                DataSet setRpt = new DataSet();

                if (setRpt == null)
                    return;
                m_DataX = new YCAEK03DOCData(dicParamater);
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                if (m_DataX.exAgent._app == null)
                {
                    m_DataX.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
                }
                m_rpt = new YCAEK03DOCReport(dicParamater);
                m_rpt.dsSource = Set4ITX;
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.OracleDB = this.OracleDB;
                m_rpt.proxy = this.proxy;
                m_rpt.IsApplyPayment = IsChkEKPBEKZ;
                m_rpt.WriteExcelReport(PrnType);
                Set4ITX.EKVRECH.Clear();
            }
        }
    }
}
